package cc.autoapi.pucong.auto.flow.core.flow.execNode.task.system;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.*;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.DataBus;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.AbstractTask;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.utils.DataValueUtils;

import java.util.ArrayList;
import java.util.List;

public class IfTask extends AbstractTask {

    private List<NodeParamIn> paramsIns;
    private List<NodeParamOut> paramsOuts;
    private Boolean booleanCheck = true;

    public IfTask() {
        paramsIns = new ArrayList<>();
        NodeParamIn nextCode = new NodeParamIn()
                .setParamCode("booleanCheck")
                .setDateType(DataTypeEnum.OBJECT)
                .setDesc("布尔判断，可以是对象、布尔值、字符串")
                .setNeedValue(true);
        paramsIns.add(nextCode);

        paramsOuts = new ArrayList<>();
    }

    @Override
    public void run() {
        ExecParamIn execParamIn = getParamInInfo("booleanCheck");
        Object dateValue = execParamIn.getDateValue();

        booleanCheck = (Boolean) DataValueUtils.transDataType("booleanCheck", DataTypeEnum.BOOLEAN, dateValue);
    }

    @Override
    public List<TaskNode> fitterNextPublishNode(List<TaskNode> nextNodeList) {
        List<TaskNodeRelation> taskNodeRelationList = DataBus.getTaskNodeRelationList(getApiId());
        TaskNodeRelation relation = taskNodeRelationList.stream().filter(item -> {
            if (booleanCheck) {
                return item.getRelationType() == 3 && item.getNodeFromId().equals(getExecNode().getUuid());
            } else {
                return item.getRelationType() == 4 && item.getNodeFromId().equals(getExecNode().getUuid());
            }
        }).findFirst().orElse(null);
        List<TaskNode> nextNodes = new ArrayList<>();
        if (relation != null) {
            String nextNodeId = relation.getNodeToId();
            for (TaskNode taskNode : nextNodeList) {
                if (nextNodeId.equals(taskNode.getUuid())) {
                    nextNodes.add(taskNode);
                }
            }
        }
        return nextNodes;
    }

    @Override
    public String getTaskName() {
        return "条件判断节点";
    }

    @Override
    public String getTaskDesc() {
        return "  本组件有一个booleanCheck输入参数，数据类型是对象，可以是任意类型数据，如果是字符串，则判断长度是否大于0，如果是数字则判断是否大于0，如果是布尔值则直接判断。\n" +
                "                如果判断的结果是true，则执行下一节点关系类型是3的节点，如果判断的结果是false则执行下一节点关系类型是4的节点。";

    }

    @Override
    public String getTaskSvgIcon() {
        return "<svg t=\"1727020682272\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"21401\" width=\"32\" height=\"32\"><path d=\"M752.355556 259.271111H285.866667c-12.8 0-21.191111 8.391111-21.191111 21.191111v466.488889c0 12.8 8.391111 21.191111 21.191111 21.191111h466.488889c12.8 0 21.191111-8.391111 21.191111-21.191111v-466.488889c0-12.8-8.391111-21.191111-21.191111-21.191111z m-427.804445 124.017778l1.137778-1.137778a23.182222 23.182222 0 0 1 32.853333 0l46.791111 46.791111 104.248889-104.248889c9.528889-9.528889 24.462222-9.528889 33.991111 0 9.528889 9.102222 9.528889 24.462222 0 33.706667l-120.604444 121.031111-0.142222 0.142222-0.284445 0.284445-0.711111 0.711111c-9.244444 8.817778-23.893333 8.675556-32.853333-0.426667l-64.284445-64a23.04 23.04 0 0 1-0.142222-32.853333z m27.448889 297.244444c-9.528889-9.528889-9.528889-24.462222 0-33.991111l299.804444-299.804444c9.528889-9.528889 24.462222-9.528889 33.991112 0 9.528889 9.528889 9.528889 24.888889 0 33.991111L385.991111 680.533333c-9.528889 9.528889-24.462222 9.528889-33.991111 0z m343.751111-22.328889c9.528889 9.528889 9.528889 24.462222 0 33.991112-9.528889 9.528889-24.462222 9.528889-33.991111 0l-29.297778-29.297778-30.72 30.72c-9.528889 9.528889-24.462222 9.528889-33.991111 0-9.528889-9.528889-9.528889-24.462222 0-33.991111l30.72-30.72-32.142222-32.142223c-9.528889-9.528889-9.528889-24.462222 0-33.991111 9.528889-9.528889 24.462222-9.528889 33.991111 0l32.142222 32.142223 30.72-30.72c9.528889-9.528889 24.462222-9.528889 33.991111 0 9.102222 9.102222 9.102222 24.462222 0 33.991111l-30.72 30.72 29.297778 29.297777zM512 0C229.262222 0 0 229.262222 0 512s229.262222 512 512 512 512-229.262222 512-512S794.737778 0 512 0z m303.928889 747.093333c0 33.991111-29.582222 63.573333-63.573333 63.573334H285.866667c-33.991111 0-63.573333-29.582222-63.573334-63.573334v-466.488889c0-33.991111 29.582222-63.573333 63.573334-63.573333h466.488889c33.991111 0 63.573333 29.582222 63.573333 63.573333v466.488889z\" fill=\"#3181FF\" p-id=\"21402\"></path></svg>";
    }

    @Override
    public List<NodeParamIn> getPropertyParamsIns() {
        return paramsIns;
    }

    @Override
    public List<NodeParamOut> getPropertyParamsOuts() {
        return paramsOuts;
    }
}
